<template>
	<!-- 对话框表单 -->
	<el-dialog
		class="ba-operate-dialog"
		:close-on-click-modal="false"
		:destroy-on-close="true"
		:model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
		@close="baTable.toggleForm"
	>
		<template #header>
			<div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
				{{ baTable.form.operate == 'Add' ? '添加' : '编辑' }}
			</div>
		</template>
		<el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
			<div
				class="ba-operate-form"
				:class="'ba-' + baTable.form.operate + '-form'"
				:style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
			>
				<el-form
					ref="formRef"
					@keyup.enter="baTable.onSubmit(formRef)"
					:model="baTable.form.items"
					:label-position="config.layout.shrink ? 'top' : 'right'"
					:label-width="baTable.form.labelWidth + 'px'"
					:rules="rules"
					v-if="!baTable.form.loading"
				>
<!--					<FormItem-->
<!--						type="remoteSelect"-->
<!--						prop="pid"-->
<!--						label="上级菜单规则"-->
<!--						v-model="baTable.form.items!.pid"-->
<!--						placeholder="点击选择"-->
<!--						:input-attr="{-->
<!--                            params: { isTree: true },-->
<!--                            field: 'title',-->
<!--                            'remote-url': '/system/menu/list',-->
<!--                        }"-->
<!--					/>-->

					<el-form-item label="上级菜单规则">
						<el-tree-select
							v-model="baTable.form.items!.pid"
							:data="select"
							@click="getSelect"
							placeholder="默认顶级"
							check-strictly
							:render-after-expand="false"
							style="width: 240px"
						/>
					</el-form-item>

					<FormItem
						label="规则类型"
						v-model="baTable.form.items!.type"
						type="radio"
						:data="{
                            content: { menu_dir: '菜单目录', menu: '菜单', button: '按钮' },
                            childrenAttr: { border: true },
                        }"
					/>
					<el-form-item prop="title" label="规则标题">
						<el-input
							v-model="baTable.form.items!.title"
							type="string"
							placeholder="请输入规则标题"
						></el-input>
					</el-form-item>
					<el-form-item prop="name" label="规则名称">
						<el-input
							v-model="baTable.form.items!.name"
							type="string"
							placeholder="请输入规则名称"
						></el-input>
						<div class="block-help">
							将注册为web端路由名称，同时作为server端API验权使用
						</div>
					</el-form-item>
					<el-form-item v-if="baTable.form.items!.type != 'button'" label="路由路径">
						<el-input
							v-model="baTable.form.items!.path"
							type="string"
							placeholder="web端路由路径(path)，无需以`/admin`开头，如:auth/menu"
						></el-input>
					</el-form-item>
					<FormItem
						v-if="baTable.form.operate && baTable.form.items!.type != 'button'"
						type="icon"
						label="规则图标"
						v-model="baTable.form.items!.icon"
						:input-attr="{ 'show-icon-name': true }"
					/>
					<FormItem
						v-if="baTable.form.items!.type == 'menu'"
						label="菜单类型"
						v-model="baTable.form.items!.menu_type"
						type="radio"
						:data="{
                            content: { tab: '选项卡', link: '链接(站外)', iframe: 'Iframe' },
                            childrenAttr: { border: true },
                        }"
					/>
					<el-form-item
						prop="url"
						v-if="baTable.form.items!.menu_type != 'tab' && baTable.form.items!.type != 'button'"
						label="链接地址"
					>
						<el-input
							v-model="baTable.form.items!.url"
							type="string"
							placeholder="请输入链接或Iframe的URL地址"
						></el-input>
					</el-form-item>
					<el-form-item
						v-if="baTable.form.items!.type == 'menu' && baTable.form.items!.menu_type == 'tab'"
						label="组件路径"
					>
						<el-input
							v-model="baTable.form.items!.component"
							type="string"
							placeholder="web端组件路径，请以/src开头，如:/src/views/backend/dashboard.vue"
						></el-input>
					</el-form-item>
					<el-form-item
						v-if="baTable.form.items!.type == 'menu' && baTable.form.items!.menu_type == 'tab'"
						label="扩展属性"
					>
						<el-select
							class="w100"
							v-model="baTable.form.items!.extend"
							placeholder=""
						>
							<el-option label="无" value="none"></el-option>
							<el-option label="只添加为路由" value="add_rules_only"></el-option>
							<el-option label="只添加为菜单" value="add_menu_only"></el-option>
						</el-select>
						<div class="block-help">
							比如将`auth/menu`只添加为路由，那么可以另外将`auth/menu`、`auth/menu/:a`、`auth/menu/:b/:c`只添加为菜单
						</div>
					</el-form-item>
					<el-form-item label="规则备注">
						<el-input
							@keyup.enter.stop=""
							@keyup.ctrl.enter="baTable.onSubmit(formRef)"
							v-model="baTable.form.items!.remark"
							type="textarea"
							:autosize="{ minRows: 2, maxRows: 5 }"
							placeholder="在控制器`get_route_Remark（）`函数中，它可以获得该字段的值供您自己使用，例如控制台的banner文件"
						></el-input>
					</el-form-item>
					<el-form-item label="规则权重">
						<el-input
							v-model="baTable.form.items!.weigh"
							type="number"
							placeholder="输入菜单规则的权重（排序依据）"
						></el-input>
					</el-form-item>
					<FormItem
						label="缓存"
						v-model.number="baTable.form.items!.keepalive"
						type="radio"
						:data="{
                            content: { 0: '禁用', 1: '启用' },
                            childrenAttr: { border: true },
                        }"
					/>
					<FormItem
						label="状态"
						v-model="baTable.form.items!.status"
						type="radio"
						:data="{
                            content: { '0': '禁用', '1': '启用' },
                            childrenAttr: { border: true },
                        }"
					/>
				</el-form>
			</div>
		</el-scrollbar>
		<template #footer>
			<div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
				<el-button @click="baTable.toggleForm('')">取消</el-button>
				<el-button
v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
						   type="primary">
					{{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? '保存并编辑下一个' : '保存' }}
				</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import {reactive, ref, inject} from 'vue'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import {buildValidatorData} from '/@/utils/validate'
import type {FormInstance, FormItemRule} from 'element-plus'
import {useConfig} from '/@/stores/config'
import createAxios from "/@/utils/axios";
import {url} from "/@/api";

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    title: [buildValidatorData({name: 'required', title: '标题'})],
    name: [buildValidatorData({name: 'required', title: '名称'})],
    url: [buildValidatorData({name: 'url', message: '输入正确的URL'})],
    pid: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val) {
                    return callback()
                }
                if (parseInt(val) == parseInt(baTable.form.items!.id)) {
                    return callback(new Error('上级菜单规则不能是规则本身'))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
})

const select = ref([{
	label: "顶级",
	value: 0,
}])

const getSelect = () => {
	select.value = [{
		label: "顶级",
		value: 0,
	}]
    createAxios({
        url: '/admin_api/menu/get_select',
        method: 'get',
    }).then((res) => {
		select.value = select.value.concat(res.data)

    })
}

</script>

<style scoped lang="scss"></style>
